<template>
  <div class="healthDetailPage">
    <!-- 原有头部、内容、分享弹窗等代码 -->
    <div class="header">
      <van-icon class="backIcon" name="arrow-left" size="30px" @click="goBack" />
      <span class="title">药品详情</span>
      <div class="headerActions">
        <van-icon
            style="margin-right:8px"
            :name="isCollected? 'star' : 'star-o'"
            size="20px"
            @click="toggleCollect"
            :style="{ color: isCollected? '#FFD700' : 'inherit' }"
        />
        <van-icon style="margin-top:2px" name="clock-o" size="20px" @click="showSharePopup = true" />
      </div>
    </div>
    <div class="content">
      <div class="drugInfo">
        <div class="drugImageWrapper">
          <img :src="article.drugImage" alt="药品图片" class="drugImage" />
        </div>
      </div>
      <div class="drugDetails">
        <span
            :class="article.isPrescription === 0? 'prescription-blue' : 'prescription-red'"
            :style="{ width: '70px', height: '30px', borderRadius: '10px' }"
        >
          {{ article.isPrescription === 0 ? '处方药' : '非处方药' }}
        </span>
        <span style="float: left; margin-left: 77px; margin-top: -27px">
          {{ article.drugName }} ({{ getAliasDisplay(article.alias) }})
        </span>
        <div style="margin-left: 2px;margin-top: 22px" class="drugSpec">
          {{ article.specification }}
        </div>
      </div>
      <div class="detailSection">
        <h3>药品信息</h3>
        <p><span>药品名称：</span>{{ article.drugName }}</p>
        <p><span>外文名：</span>{{ article.foreignName }}</p>
        <p><span>别名：</span>{{ article.alias }}</p>
        <p>
          <span>是否处方药：</span>
          {{ article.isPrescription === 0 ? '是' : '否' }}，主要适用症用于
          {{ article.indications }}
        </p>
      </div>
      <div class="detailSection">
        <h3>功能主治</h3>
        <p>{{ article.functionAndUsage }}</p>
      </div>
    </div>
    <div v-if="showSharePopup" class="sharePopup">
      <div class="popupHeader">分享</div>
      <div class="shareOptions">
        <div class="shareOption" @click="share('wechat')">
          <img src="wechat-icon.png" alt="微信" />
          <span>微信</span>
        </div>
        <div class="shareOption" @click="share('moments')">
          <img src="moments-icon.png" alt="朋友圈" />
          <span>朋友圈</span>
        </div>
        <div class="shareOption" @click="share('qq')">
          <img src="qq-icon.png" alt="QQ" />
          <span>QQ</span>
        </div>
      </div>
      <div class="popupFooter" @click="showSharePopup = false">取消</div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';
import {Toast} from "vant";
export default {
  data() {
    return {
      article: {},
      showSharePopup: false,
      isCollected: false,
      status: null // 新增 status 字段用于存储收藏状态
    };
  },
  methods: {
    findDrugInfo(id) {
      axios
          .get(`http://localhost:10086/api/doctor/drug_info/SearchDrugInfoById/${id}`)
          .then(res => {
            console.log(res);
            this.article = res.data.data;
            // 从响应数据中获取 status 字段
            this.status = this.article.status;
            // 根据 status 初始化 isCollected
            this.isCollected = this.status === 0;
          })
          .catch(err => {
            console.log(err);
          });
    },
    goBack() {
      this.$router.go(-1);
    },
    share(platform) {
      console.log(`分享到 ${platform}`);
      this.showSharePopup = false;
    },
    async toggleCollect() {
      try {
        const articleId = this.article.id;
        if (this.isCollected) {

          // 取消收藏
          await axios.post(`http://localhost:10086/api/doctor/drug_info/collect/${articleId}`);
          this.isCollected = false;
          this.status = 1; // 更新 status 为未收藏状态
          Toast.success({
            message: '取消收藏',
            position: 'middle'
          });
        } else {
          // 收藏
          await axios.post(`http://localhost:10086/api/doctor/drug_info/collect/${articleId}`);
          this.isCollected = true;
          this.status = 0; // 更新 status 为已收藏状态
          Toast.success({
            message: '收藏成功',
            position: 'middle'
          });
        }
      } catch (error) {
        console.error('收藏操作失败', error);
      }
    },
    getAliasDisplay(alias) {
      if (alias.length > 5) {
        return alias.slice(0, 5) + '...';
      }
      return alias;
    }
  },
  created() {
    const articleId = this.$route.params.id;
    if (articleId) {
      this.findDrugInfo(articleId);
    }
  }
};
</script>

<style scoped>
.healthDetailPage {
  background-color: #f7fafa;
  min-height: 100vh;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 50px;
  padding: 0 15px;
  background-color: white;
  /*box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);*/
}

.backIcon {
  cursor: pointer;
}

.title {
  font-size: 18px;
  font-weight: 700;
}

.headerActions {
  display: flex;
  align-items: center;
}

.headerActions van-icon {
  margin-left: 10px;
  cursor: pointer;
}

.content {
  padding: 20px;
}

.drugInfo {
  display: flex;
  margin-bottom: 20px;
  background-color: #e6f2ff;
  padding: 15px;
  border-radius: 10px;
}

.drugImageWrapper {
  /*width: 100px; 调整图片容器宽度 */
  /*height: 100px; 调整图片容器高度 */
  display: flex;
  align-items: center;
  justify-content: center;
}

.drugImage {
  width: 200px;
  margin-left: 75px;
  max-width: 100%;
  max-height: 100%;
}

.drugDetails {
  display: flex;
  flex-direction: column;
  justify-content: center;
  margin-left: 15px;
  /* 新增以下样式 */
  font-weight: 600; /* 使字体更粗 */
  color: #333; /* 使字体颜色更深 */
}

.prescription-red {
  background-color: rgba(255, 0, 0, 0.1);
  color: red;
  text-align: center;
  line-height: 30px;
  font-weight: 400;
}

.prescription-blue {
  background-color: rgba(0, 0, 255, 0.3);
  color: blue;
  text-align: center;
  line-height: 30px;
}

.drugSpec {
  color: #999;
  font-size: 12px;
}

.detailSection {
  margin-bottom: 20px;
  margin-left: 20px;
}

.detailSection h3 {
  font-size: 20px;
  margin-bottom: 10px;
}

.detailSection p {
  line-height: 1.6;
  margin-bottom: 5px;
}

.sharePopup {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  background-color: #fff;
  box-shadow: 0 -1px 3px rgba(0, 0, 0, 0.1);
  padding: 20px;
  z-index: 100;
}

.popupHeader {
  text-align: center;
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 20px;
}

.shareOptions {
  display: flex;
  justify-content: space-around;
  margin-bottom: 20px;
}

.shareOption {
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: pointer;
}

.shareOption img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: #007AFF;
  padding: 10px;
}

.popupFooter {
  text-align: center;
  font-size: 16px;
  color: #999;
  cursor: pointer;
}
</style>